﻿using System.Threading.Tasks;
using System.Web.Mvc;
using SDCloudSolution.SDCloud.Web.Infrastructure.Interfaces;
using SDCloudSolution.SDCloud.Web.Models;

namespace SDCloudSolution.SDCloud.Web.Controllers
{

    [RoutePrefix("gallery/models")]
    [Authorize]
    public partial class ModelsGalleryController : Controller
    {
        private readonly IModelViewModelProvider _modelViewModelProvider;

        public ModelsGalleryController(IModelViewModelProvider modelViewModelProvider)
        {
            _modelViewModelProvider = modelViewModelProvider;
        }

        [Route]
        [HttpGet]
        public virtual async Task<ActionResult> List()
        {
            var pageModel = await _modelViewModelProvider.GetPublicModelsListViewModelAsync(PagingModel.Default);

            return View(pageModel);
        }

        [Route("{publicModelId}")]
        [HttpGet]
        public virtual async Task<ActionResult> View(int publicModelId)
        {
            var pageModel = await _modelViewModelProvider.GetPublicModelViewModelAsync(publicModelId);

            return View(pageModel);
        }

        [Route("{publicModelId}/copy")]
        [HttpPost]
        public virtual async Task<ActionResult> CopyModelToPrivateCollection(int publicModelId)
        {
            var copyModelId = await _modelViewModelProvider.CopyPublicModelToPrivateCollection(publicModelId);

            return copyModelId > 0
                ? Redirect(Url.Action(MVC.Models.View(copyModelId)))
                : Redirect(Url.Action(MVC.ModelsGallery.List()));
        }
    }
}